/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package au.com.gworks.jump.app.wiki.client.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializableException;

public interface WikiRpc extends RemoteService {
	public String SERVICE_CTX = "/jump.wiki";
	
	final static public int OPEN_RESOURCE_CTX = 0; 
	final static public int EDIT_RESOURCE_CTX = 1; 
	final static public int EXPLORE_CTX = 2; 
	final static public int SEARCH_CTX = 3; 
	final static public int LOG_HISTORY_CTX = 4; 
	final static public int CHANGESET_REPORT_CTX = 5; 
	final static public int DIFFERENCE_REPORT_CTX = 6; 
	final static public int LIST_PROPERTIES_CTX = 7; 
	final static public int EDIT_PROPERTIES_CTX = 8; 
	final static public int RESERVED_LOCKS_REPORT_CTX = 9; 
	final static public int UNSPECIFIED_CTX = 10; 
	
	public MyspacesInfo openMyspaces()
			throws SystemFailureException;
	
	// read support
	public ExplorerInfo exploreFolder(String namespace, Integer revCtx,
			String folderPath) throws UnauthorisedAccessException,
			ResourceNotFoundException, SystemFailureException;

	public SearchInfo searchResources(String lookIn, String criteria,
			long date1, long date, boolean inclSubFolders,
			boolean inclAllMatchingRevs) throws UnauthorisedAccessException,
			ResourceNotFoundException, SystemFailureException;

	public LogHistoryInfo logHistory(String namespace, String resource,
			Integer fromRev, Integer toRev, boolean inclHierarchy) throws UnauthorisedAccessException,
			ResourceNotFoundException, SystemFailureException;
	
	public RevisionChangesInfo reportRevisionChanges(String namespace, Integer revision)
			throws UnauthorisedAccessException, SystemFailureException;

	public ResourceDiffInfo reportRevisionDifferences(String namespace, String resource, Integer asOfRev, Integer revA, Integer revB)
		throws UnauthorisedAccessException, ResourceNotFoundException, SystemFailureException;
	
	// write interface
	public class RequestPathInfo implements IsSerializable {
		public String revSpecifier;
		
		public String baselineRevision;

		public String namespace, context;

		public String path;

		public Integer revision, headRevision;

		public int pathStatus;

		public boolean wasRevisionSpecified;

		public boolean isNamespaceAdmin;

		public ResourceAttrInfo resourceAttributes;
	}
	
	public class ResourceAttrInfo implements IsSerializable {
		public String name;

		public String parentPath;

		public Integer revision;

		public long lastModified;

		public String lastUpdater;

		public Integer lastChangedRevision;

		public long size;

		public String uuid;

		/**
		 * @gwt.typeArgs <java.lang.String,java.lang.String>
		 */
		public Map properties = new HashMap();
		
		public String getAbsolutePath() {
			String ppWithSlash = parentPath;
			if (!ppWithSlash.endsWith("/"))
				ppWithSlash += "/";
			return ppWithSlash + name;
		}
	}
	
	public class MyspacesInfo implements IsSerializable {
		public Item[] items;
		
		static public class Item implements IsSerializable {
			public String namespace;
			public Integer revision;
			public long lastModified;
			public String lastUpdater;
		}
	}

	public class SearchInfo implements IsSerializable {
		public Item[] items;

		static public class Item implements IsSerializable {
			public String namespace;

			public float score;

			public String summary;

			public Integer timedRevision;

			public long timedDate;

			public String lastUpdater;

			/**
			 *  @gwt.typeArgs <java.lang.String,java.lang.String>
			 */
			public Map storedProperties;
		}
	}

	public class ExplorerInfo implements IsSerializable {
		public String namespace;

		public Integer timedRevision;

		public long timedDate;

		public int permission;

		public String parentPath;

		public String folderPath;

		public String[] subFolders;

		public ResourceAttrInfo[] resources;
	}
	
	public class LogHistoryInfo implements IsSerializable {
		public String namespace;

		public Integer fromRevision;

		public Integer toRevision;
		
		public Item[] items;	// this could be either CommitInfos or Hierarchy histories

		static public class Item implements IsSerializable {
			public Integer revision;

			public String author;

			public long changeDate;

			public String changeComment;
		}
		
		static public class CommitItem extends Item {
		}

		static public class HierarchyHistoryItem extends Item {
			public String resourcePath;
		}
	}

	public class RevisionChangesInfo implements IsSerializable {
	    public String resourcePath;
	    public Integer revision;
	    public String changeType;
	    public String copyFromPath;
	    public Integer copyFromRevision;
	}
	
	public class ResourceDiffInfo implements IsSerializable {
		public String namespace;

		public Integer atRevision;
		
		public Integer revisionA;

		public Integer revisionB;
		
		/**
		 * @gwt.typeArgs <java.lang.String>
		 */
		public List revAdifferencesList;

		/**
		 * @gwt.typeArgs <java.lang.String>
		 */
		public List revBdifferencesList;
	}
	
	public class UnauthorisedAccessException extends SerializableException {
		private static final long serialVersionUID = 1L;

		public String requestedPath;

		public UnauthorisedAccessException() {}
		public UnauthorisedAccessException(String msg, String reqPath) {
			super(msg);
			requestedPath = reqPath;
		}
	}
	
	public class ResourceNotFoundException extends ResourceConflictException {
		private static final long serialVersionUID = 1L;

		public String parentPath;

		public String rootCauseStackTrace;

		public ResourceNotFoundException() {}
		public ResourceNotFoundException(String msg, String resPath,
				Integer rev, String prntPath, String rootCause) {
			super(msg, resPath, rev);
			parentPath = prntPath;
			rootCauseStackTrace = rootCause;
		}
	}

	abstract public class ResourceConflictException extends SerializableException {
		private static final long serialVersionUID = 1L;

		public String resourcePath;

		public Integer revision;

		public ResourceConflictException() {}
		public ResourceConflictException(String msg, String resPath, Integer rev) {
			super(msg);
			resourcePath = resPath;
			revision = rev;
		}
	}

	public class SystemFailureException extends SerializableException {
		private static final long serialVersionUID = 1L;

		public String rootCauseStackTrace;

		public SystemFailureException() {}
		public SystemFailureException(String msg, String rootCause) {
			super(msg);
			rootCauseStackTrace = rootCause;
		}
	}
	
	public class CompilationException extends SerializableException {
		private static final long serialVersionUID = 1L;

		public ResourceAttrInfo source;

		public String rootCauseStackTrace;

		public CompilationException() {}
		public CompilationException(String msg, ResourceAttrInfo src,
				String rootCause) {
			super(msg);
			source = src;
			rootCauseStackTrace = rootCause;
		}
	}

}
